package com.ocr.camera;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.OnZoomChangeListener;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements
		SurfaceHolder.Callback {
	public static String TAG = "camera";
	private SurfaceHolder mHolder;
	public Camera camera;

	public CameraPreview(Context context) {
		super(context);

		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

	}

	public void surfaceCreated(SurfaceHolder holder) {

		camera = getCameraInstance();
		try {
			if (camera != null) {
				camera.setPreviewDisplay(holder);
				camera.startPreview();

				// set camera parameter
				Camera.Parameters parameters = camera.getParameters();
				List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
				//int count = 0;
				//for (Camera.Size size : sizes) {
				//	Log.i("Size"+count++, size.width+" "+size.height);
				//}
				Camera.Size size = sizes.get(sizes.size()-1);
				//if(sizes.size() > 1)
				//	size = sizes.get(1);
				parameters.setPictureSize(size.width, size.height);
				parameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);
				parameters.setPreviewSize(this.getWidth(), this.getHeight());
				parameters.setFocusMode(Camera.Parameters.FLASH_MODE_AUTO);
				parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
				parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
				//Log.i("Size", this.getWidth() + " " + this.getHeight());
				camera.setParameters(parameters);
			}
		} catch (IOException e) {
			Log.d(TAG, "Error setting camera preview: " + e.getMessage());
		}

	}

	public void surfaceDestroyed(SurfaceHolder holder) {

		camera.stopPreview();
		camera.release();
		camera = null;

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// If your preview can change or rotate, take care of those events here.
		// Make sure to stop the preview before resizing or reformatting it.

		if (mHolder.getSurface() == null) {
			// preview surface does not exist
			return;
		}

		// stop preview before making changes
		try {
			camera.stopPreview();
		} catch (Exception e) {
			// ignore: tried to stop a non-existent preview
		}

		// make any resize, rotate or reformatting changes here
		Camera.Parameters param = camera.getParameters();
		List<Camera.Size> sizes = param.getSupportedPictureSizes();
		//for (Camera.Size size : sizes) {
		//	Log.i("Size", size.width+" "+size.height);
		//}
		Camera.Size size = sizes.get(sizes.size()-1);
		//if(sizes.size() > 1)
		//	size = sizes.get(1);
		param.setPictureSize(size.width, size.height);
		param.setPreviewSize(w, h);
		param.setPreviewFormat(PixelFormat.YCbCr_420_SP);
		param.setFocusMode(Camera.Parameters.FLASH_MODE_AUTO);
		param.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
		param.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
		camera.setParameters(param);
		// start preview with new settings
		try {
			camera.setPreviewDisplay(mHolder);
			camera.startPreview();

		} catch (Exception e) {
			Log.d(TAG, "Error starting camera preview: " + e.getMessage());
		}
	}

	/** A safe way to get an instance of the Camera object. */
	public Camera getCameraInstance() {
		Camera c = null;
		try {
			c = Camera.open(); // attempt to get a Camera instance

			// initial camera
			c.getParameters().setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
			c.autoFocus(autofocus);
			c.setZoomChangeListener(zoomchange);
			c.startSmoothZoom(c.getParameters().getZoom());
		} catch (Exception e) {
			// Camera is not available (in use or does not exist)
		}
		return c; // returns null if camera is unavailable
	}

	// camera autofocus callback
	public AutoFocusCallback autofocus = new AutoFocusCallback() {

		@Override
		public void onAutoFocus(boolean success, Camera camera) {
			// TODO Auto-generated method stub

		}
	};

	public OnZoomChangeListener zoomchange = new OnZoomChangeListener() {

		@Override
		public void onZoomChange(int zoomValue, boolean stopped, Camera camera) {
			// TODO Auto-generated method stub
			Log.i("camera", "zoom change");
			camera.startSmoothZoom(zoomValue);
		}
	};

}
